﻿@* Quang: 29/05/2015: viet _partialMenu :: Tao ra the navigator va cac the ul li cho menu dong*@
@*<ul><li>@ViewBag.IsAuthen</li></ul>*@
<ul id="nav_p1" class="nav navbar-nav">
    @{
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (Session["MenuList"] != null)
            {
                List<DHQGHN.PM2.Models.MenuModel> listMenu = Session["MenuList"] as List<DHQGHN.PM2.Models.MenuModel>;//ViewBag.Menu;//new List<DHQGHN.PM2.Models.MenuModel>();
                if (listMenu.Count > 0)
                {
                    foreach (var mp in listMenu.Where(p => p.ParentMenuId == 0))
                    {
                        string[] spl = SplitUrl(mp.MenuUrl);
                        <li class="dropdown">
                            @*cho nay se thay sang the tag <a>*@
                            @*@Html.ActionLink(mp.MenuName, mp.MenuName, mp.MenuName)*@
                            @Html.ActionLink(mp.MenuName, spl[2], spl[1])
                            <ul class="dropdown-menu" role="navigation">
                                @RenderMenuItem(listMenu, mp)
                            </ul>
                        </li>
                    }
                }
            }
            //}
            @*<li>@Html.ActionLink("Logout", "Logout", "Account")</li>
                <li><strong>@User.Identity.Name</strong></li>*@
        }
        else
        {
            <li>@Html.ActionLink("Login", "Login", "Account")</li>
            <li>@Html.ActionLink("Register", "Register", "Account")</li>
        }
    }
    <li></li>
</ul>

@** ham de quy de tao ra menu con*@
@helper RenderMenuItem(List<DHQGHN.PM2.Models.MenuModel> menuList, DHQGHN.PM2.Models.MenuModel mi)
{
    foreach (var cp in menuList.Where(p => p.ParentMenuId == mi.MenuId))
    {
        string[] spl = SplitUrl(cp.MenuUrl);

        <li>
            @*@Html.ActionLink(cp.MenuName, cp.MenuName, cp.MenuName)*@
            @Html.ActionLink(cp.MenuName, spl[2], spl[1], routeValues: null, htmlAttributes: new { @id = "cName"})
            
            @*<ul>
                @RenderMenuItem(menuList, cp)
            </ul>*@
        </li>
    }
}

@*ham cat chuoi url*@
@functions{
    public string[] SplitUrl(string urlString)
    {
        string[] word;
        if (urlString != "#")
        {
            return word = urlString.Split('/');
            //return word;
        }
        else //return word = urlString.Split('#');
            return word = new string[] { "", "", "" };
    }
}
